﻿@model Cxuu.Models.EditAdminUser
@{
    ViewData["Title"] = "保存";
}
<div class="content-top-nav">
    <div class="layui-row">
        <div class="layui-col-md11">
            <h3 style="color:#444">
                <i class="layui-icon layui-icon-home"></i>
                用户编辑
            </h3>
        </div>
        <div class="layui-col-md1" style="text-align:right;">
            <button class="layui-btn layui-btn-sm" onclick="loadMain('/Admin/User/')">返回列表</button>
        </div>
    </div>
    <hr>
</div>

<div class="layui-fluid" style="margin-bottom:50px;">
    <div class="layui-form">
        <form class="layui-form" action="" lay-filter="FormEdit" enctype="multipart/form-data">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>

            <div class="layui-form-item">
                <label class="layui-form-label required">选择用户组</label>
                <div class="layui-input-inline">
                    <select name="Gid" lay-verify="">
                        @foreach (var item in ViewBag.adminUserGroup)
                        {
                            <option value="@item.Id">@item.Groupname</option>
                        }
                    </select>
                </div>
            </div>

            <div class="layui-form-item">
                <label asp-for="Username" class="layui-form-label"></label>
                <div class="layui-input-inline">
                    <input type="text" asp-for="Username" lay-verify="required" lay-reqtext="用户名不能为空" placeholder="请输入用户名" value="" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label asp-for="Password" class="layui-form-label"></label>
                <div class="layui-input-inline">
                    <input type="text" asp-for="Password" placeholder="请输入密码" value="" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux">新增用户密码不能为空,修改用户密码为空，即不修改密码</div>
            </div>

            <div class="layui-form-item">
                <label asp-for="Nickname" class="layui-form-label"></label>
                <div class="layui-input-inline">
                    <input type="text" asp-for="Nickname" lay-verify="required" lay-reqtext="昵称不能为空" placeholder="请输入昵称" value="" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label asp-for="Status" class="layui-form-label"></label>
                <div class="layui-input-inline">
                    <input asp-for="Status" type="radio" value="true" title="启用" checked>
                    <input asp-for="Status" type="radio" value="false" title="禁用">
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit lay-filter="saveBtn" id="saveBtn">@ViewData["Title"]</button>
                </div>
            </div>
        </form>
    </div>
</div>
<script>
    layui.use(['form', 'table', 'upload', 'jquery'], function () {
        var form = layui.form, layer = layui.layer, $ = layui.jquery;

        form.render();

        var editId = @ViewBag.id+1;
        //console.log(editId);
        if (editId > 1) {
            $.getJSON("/Admin/User/AddOrEdit?id=@ViewBag.id&edit=1", function (res) {
                if (res.status == 1) {
                    form.val('FormEdit', {
                        Id: res.data.id,
                        Gid: res.data.gid,
                        Username: res.data.username,
                        //Password: res.data.password,
                        Nickname: res.data.nickname,
                        Status: res.data.status,
                    });
                    if (res.data.status)
                        $('input[name="Status"][value="true"]').prop('checked', 'checked');
                    else
                        $('input[name="Status"][value="false"]').prop('checked', 'checked');
                    form.render('radio');
                }
                //console.log("json", res+"abc");
            })
        } else {
            $('input[name="Status"][value="true"]').prop('checked', 'checked');
            form.render('radio');
         }

        var dataPost = function (url,data) {
            $.post(url, data, function (res) {
                if (res.status == 1) {
                    layer.msg(res.msg, { icon: 1, time: 1000 }, function () {
                        loadMain('/Admin/User/Index/');
                    });
                } else {
                    layer.msg(res.msg, { icon: 2, anim: 6, time: 5000 });
                }
            });
        }

        //监听提交
       // form.on('submit(saveBtn)', function (data) {
        $('#saveBtn').click(function () {
            var post = form.val('FormEdit');

            //var post = {
            //    Gid: data.field.Gid,
            //    Username: data.field.Username,
            //    Nickname: data.field.Nickname,
            //    Password: data.field.Password,
            //    Status: data.field.Status,
            //};
            if (editId == 1)
                dataPost('/Admin/User/CreatePost', post);
             else
                dataPost('/Admin/User/EditPost?id=@ViewBag.id', post);
            return false;
        });


    });

</script>